﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using HCMServices.DTO;
using System.Collections.Generic;

namespace HCMServices.DAO
{
    /// <summary>
    /// Class to process for TINHTHANH Table
    /// </summary>
    public class CityDAO
    {
        private MyDataContext db;

        /// <summary>
        /// Constructor of this class
        /// </summary>
        /// <param name="connectString">String identify how to connect database</param>
        public CityDAO(string connectString)
        {
            db = new MyDataContext(connectString);
        }

        /// <summary>
        /// Function to get information of city from its id
        /// </summary>
        /// <param name="id">Id of city in database</param>
        /// <returns>Row represents the city if id was found or null</returns>
        public CityDTO Search(int id)
        {
            CityDTO tinhThanhDTO = new CityDTO();

            IList<TINHTHANH> query = (from TINHTHANH tinh in db.City
                                     where tinh.MaTinhThanh == id
                                     select tinh).ToList();
            
            foreach (TINHTHANH t in query)
            {
                tinhThanhDTO.Id = t.MaTinhThanh;
                tinhThanhDTO.Name = t.TenTinhThanh;
                return tinhThanhDTO;                       
            }

            return null;                                   
        }

        /// <summary>
        /// Function to get all cities
        /// </summary>
        /// <returns>List of city was found in database</returns>
        public List<CityDTO> GetAllCityDTO()
        {
            List<CityDTO> lstCityDTO = new List<CityDTO>();

            IList<TINHTHANH> query = (from TINHTHANH tt in db.City
                                      select tt).ToList();

            foreach (TINHTHANH tt in query)
            {
                CityDTO temp = new CityDTO();
                temp.Id = tt.MaTinhThanh;
                temp.Name = tt.TenTinhThanh;

                lstCityDTO.Add(temp);
            }

            return lstCityDTO;
        }
    }
}
